class Camera {
  float x, y, w, h;
  float halfW, halfH;
  float MIN_W, MIN_H, MAX_W, MAX_H;
  float s;
  float MIN_SCALE, MAX_SCALE;
  
  Camera(float x, float y) {
    this.x = x;
    this.y = y;
    this.w = width;
    this.h = height;
    this.halfW = this.w * 0.5;
    this.halfH = this.h * 0.5;
    this.MIN_W = width;
    this.MIN_H = height;
    this.MAX_W = 2 * width;
    this.MAX_H = 2 * height;
    this.s = 1;
    this.MIN_SCALE = 1;
    this.MAX_SCALE = 2;
  }
  
  void translateCamera(float x, float y) {
    this.x = x;
    this.y = y;
  }
  
  void scaleCamera(float s) {
    this.s = constrain(s, this.MIN_SCALE, this.MAX_SCALE);
    this.w = constrain(this.s * this.MIN_W, this.MIN_W, this.MAX_W);
    this.h = constrain(this.s * this.MIN_H, this.MIN_H, this.MAX_H);
    this.halfW = this.w * 0.5;
    this.halfH = this.h * 0.5;
  }
  
  void constrainCamera() {
    this.x = constrain(this.x, this.halfW, this.MAX_W - this.halfW);
    this.y = constrain(this.y, this.halfH, this.MAX_H - this.halfH);
  }
  
  void apply() {
    scale(1 / this.s);
    translate(-this.x + this.halfW, -this.y + this.halfH);
  }
}
